package com.lzb.permanentcalendar.utils;

import android.util.Log;

import java.util.Calendar;

public class DateCalculator {
    
    /**
     * 获取前一天的年月日
     */
    public static int[] getPreviousDay(int year, int month, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, day); // month从0开始，所以要减1
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        Log.e("TAG", "getPreviousDay: "+month+" "+calendar.get(Calendar.MONTH)+1 );
        
        return new int[] {
            calendar.get(Calendar.YEAR),
            calendar.get(Calendar.MONTH) + 1, // 加1返回正常的月份
            calendar.get(Calendar.DAY_OF_MONTH)
        };
    }
    
    /**
     * 获取后一天的年月日
     */
    public static int[] getNextDay(int year, int month, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, day);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        
        return new int[] {
            calendar.get(Calendar.YEAR),
            calendar.get(Calendar.MONTH) + 1,
            calendar.get(Calendar.DAY_OF_MONTH)
        };
    }
    
    /**
     * 获取前一天和后一天（返回二维数组）
     */
    public static int[][] getPreviousAndNextDay(int year, int month, int day) {
        int[] previous = getPreviousDay(year, month, day);
        int[] next = getNextDay(year, month, day);
        
        return new int[][] {previous, next};
    }
    
    /**
     * 获取今天距离今年结束还有多少天
     */
    public static int getDaysUntilEndOfYear() {
        Calendar today = Calendar.getInstance();
        Calendar endOfYear = Calendar.getInstance();
        
        // 设置为今年的最后一天 12月31日
        endOfYear.set(Calendar.MONTH, Calendar.DECEMBER);
        endOfYear.set(Calendar.DAY_OF_MONTH, 31);
        endOfYear.set(Calendar.HOUR_OF_DAY, 23);
        endOfYear.set(Calendar.MINUTE, 59);
        endOfYear.set(Calendar.SECOND, 59);
        endOfYear.set(Calendar.MILLISECOND, 999);
        
        // 计算相差的天数
        long diffMillis = endOfYear.getTimeInMillis() - today.getTimeInMillis();
        return (int) (diffMillis / (24 * 60 * 60 * 1000)) + 1; // 加1包含今天
    }
    
    /**
     * 根据指定年月日计算距离今年结束还有多少天
     */
    public static int getDaysUntilEndOfYear(int year, int month, int day) {
        Calendar specifiedDate = Calendar.getInstance();
        specifiedDate.set(year, month - 1, day);
        
        Calendar endOfYear = Calendar.getInstance();
        endOfYear.set(Calendar.YEAR, year); // 使用指定的年份
        endOfYear.set(Calendar.MONTH, Calendar.DECEMBER);
        endOfYear.set(Calendar.DAY_OF_MONTH, 31);
        endOfYear.set(Calendar.HOUR_OF_DAY, 23);
        endOfYear.set(Calendar.MINUTE, 59);
        endOfYear.set(Calendar.SECOND, 59);
        endOfYear.set(Calendar.MILLISECOND, 999);
        
        long diffMillis = endOfYear.getTimeInMillis() - specifiedDate.getTimeInMillis();
        return (int) (diffMillis / (24 * 60 * 60 * 1000)) + 1;
    }
    
    /**
     * 计算两个时间戳之间相隔的天数
     * @param timestamp1 第一个时间戳（毫秒级）
     * @param timestamp2 第二个时间戳（毫秒级）
     * @return 相隔的天数
     */
    public static int getDaysBetweenTimestamps(long timestamp1, long timestamp2) {
        long diff = timestamp1 - timestamp2;
        return (int) (diff / (24 * 60 * 60 * 1000));
    }

    /**
     * 计算两个时间戳之间相隔的天数（精确计算，考虑时区）
     * @param timestamp1 第一个时间戳（毫秒级）
     * @param timestamp2 第二个时间戳（毫秒级）
     * @return 相隔的天数
     */
    public static int getDaysBetweenExact(long timestamp1, long timestamp2) {
        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        
        calendar1.setTimeInMillis(timestamp1);
        calendar2.setTimeInMillis(timestamp2);
        
        // 重置时分秒毫秒为0，只比较日期部分
        calendar1.set(Calendar.HOUR_OF_DAY, 0);
        calendar1.set(Calendar.MINUTE, 0);
        calendar1.set(Calendar.SECOND, 0);
        calendar1.set(Calendar.MILLISECOND, 0);
        
        calendar2.set(Calendar.HOUR_OF_DAY, 0);
        calendar2.set(Calendar.MINUTE, 0);
        calendar2.set(Calendar.SECOND, 0);
        calendar2.set(Calendar.MILLISECOND, 0);
        
        long diff = calendar1.getTimeInMillis() - calendar2.getTimeInMillis();
        return (int) (diff / (24 * 60 * 60 * 1000));
    }
}